package org.jdsm.gae.komarichyn.blog;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User: mikom
 * Date: 11/13/12
 * Time: 2:01 PM
 */
public class FileManagerAction extends ActionSupport {
    private static final Logger log = LoggerFactory.getLogger(FileManagerAction.class);

    private String upload;
    private String uploadContentType;
    private String uploadFileName;
    private String uploadFolderName;
    private InputStream uploadStream;
    private File file;
    private int uploadContentLength;
    private List<File> files = new ArrayList<File>();
    private List<Folder> dirs = new ArrayList<Folder>();
    private String fileKey;

    private Mode mode;

    public String view() throws Exception{
        FileManagerHelper helper = new FileManagerHelper();
        byte[] b = helper.findByKey(fileKey);
        uploadStream = new ByteArrayInputStream(b);
        uploadContentType = "image/png";
        return SUCCESS;
    }

    public String upload() throws Exception {
        uploadStream = IOUtils.toInputStream(upload, "ISO-8859-1");
        FileManagerHelper helper = new FileManagerHelper();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int read;
        while ((read = uploadStream.read()) != -1) {
            baos.write(read);
        }

        final Image image = ImagesServiceFactory.makeImage(baos.toByteArray());

        file = new File();
        file.setName(uploadFileName);
        file.setDate(new Date());
        file.setExt("png");
        file.setMtime(1223123123);
        file.setHeight(32);
        file.setWidth(32);
        file.setR_size(image.getImageData().length);
        file.setSize(image.getImageData().length + " b");
        file.setThumb("/root");



        helper.createFile(file, null, baos);
        return SUCCESS;
    }

    public String browse() throws Exception {
        FileManagerHelper helper = new FileManagerHelper();
        helper.buildKey();
        if (mode != null) {
            switch (mode.modeValue()) {
                case Mode.CONFIG_MODE: {
                    return "cfg";
//                    break;
                }
                case Mode.DIR_MODE: {
                    return "getDirs";
//                    break;
                }
                case Mode.FILE_MODE: {
                    files = helper.getFiles();
//                    return "getFiles";

                    break;
                }
            }
        }

        return SUCCESS;
    }


    public String getUpload() {
        return upload;
    }

    public void setUpload(String upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public InputStream getUploadStream() {
        return uploadStream;
    }

    public void setUploadStream(InputStream uploadStream) {
        this.uploadStream = uploadStream;
    }

    public String getUploadFolderName() {
        return uploadFolderName;
    }

    public void setUploadFolderName(String uploadFolderName) {
        this.uploadFolderName = uploadFolderName;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public int getUploadContentLength() {
        return uploadContentLength;
    }

    public void setUploadContentLength(int uploadContentLength) {
        this.uploadContentLength = uploadContentLength;
    }


    public Mode getMode() {
        return mode;
    }

    public void setMode(Mode mode) {
        this.mode = mode;
    }

    public List<File> getFiles() {
        return files;
    }

    public void setFiles(List<File> files) {
        this.files = files;
    }

    public List<Folder> getDirs() {
        return dirs;
    }

    public void setDirs(List<Folder> dirs) {
        this.dirs = dirs;
    }

    public String getFileKey() {
        return fileKey;
    }

    public void setFileKey(String fileKey) {
        this.fileKey = fileKey;
    }
}



